<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>H同人</title>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
}

.container {
	width: 930px;
	margin-left: auto;
	margin-right: auto;
	margin-top: 10px;
}

.img_div {
	float: left;
	text-align: center;
}

.img_div .img_block {
	width: 300px;
	height: 200px;
}

.img_div .img_block .img {
	max-height: 100%;
	max-width: 100%;
}

.img_div  .name {
	margin: 5px;
	text-align: center;
	height: 90px;
	font-weight: bolder;
	color: blue;
	width: 300px;
}

.page {
	text-align: center;
}

.clear {
	clear: both;
}
</style>

<script type="text/javascript" src="../jquery-3.4.1.min.js"></script>
<script type="text/javascript">
	function search() {
		$("#pageNum").val(1);
		$("#html").html("");
		load();
	}

	function clean() {
		$("#keyword").val("");
		$("#pageNum").val(1);
		$("#html").html("");
		load();
	}

	function load() {
		$.ajax({
			type : 'POST',
			url : 'doujin',
			data : {
				keyword : $("#keyword").val(),
				pageNum : $("#pageNum").val(),
				type : $("#type").val(),
				pageSize : 50
			},
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					var doujins = data.obj;
					
					var html = "";
					for(let i=0; i<doujins.length; i++){
						var doujin = doujins[i];
						html += `
							<div class="img_div">
								<div class="img_block">
									<img class="img" src="doujin/cover/${doujin.id}.jpg" onclick="see('${doujin.id}')" style="cursor: pointer;">
								</div>
								<div class="name">${doujin.name}</div>
							</div>
						`;
					}
					
					$("#html").append(html);
					
				} else {
					alert(data.msg);
				}
			},
			error : function() {
				alert("请求失败，请刷新重试");
			}
		});
	}

	function see(id) {
		window.open('doujin/see?id=' + id);
	}

	window.addEventListener('scroll',
		function() {
			if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 200) { // 当接近页面底部时
				var current = parseInt($("#pageNum").val()) + page;
				$("#pageNum").val(current);
				
				load();
			}
		});
</script>
</head>
<body>

	<div style="text-align: center;">
		<form method="post" action="doujin" id="form">

			<input type="hidden" id="pageNum" name="pageNum" value="1" />
			<input type="hidden" id="type" name="type" value="${type}" />

			<input type="text" name="keyword" id="keyword" style="width: 300px; height: 30px;">
			<button type="button" onclick="search()" style="width: 50px; height: 34px;">Search</button>
			<button type="button" onclick="clean()" style="width: 50px; height: 34px;">Clean</button>
		</form>
	</div>

	<div class="container">
		<div id="html"></div>

		<div class="clear"></div>
	</div>

</body>
</html>
